//
//  String+Regex.swift
//  02-正则表达式抽取
//
//  Created by 程雷 on 15/7/13.
//  Copyright © 2015年 程雷. All rights reserved.
//

import Foundation

extension String {

    // 提取一个字符串中第一个 herf 链接
    func herfLink() ->(link:String,text:String)? {
        let pattern = "<a.*?\"(.*?)\".*?>(.*?)</a>"
        
        do{
            let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.DotMatchesLineSeparators)
            
            //开始匹配
            let result = regex.firstMatchInString(self, options: NSMatchingOptions(rawValue: 0), range: NSRange(location: 0, length: self.characters.count))
            
            // 判断是否找到匹配项
            if result != nil
            {
                let r1 = result!.rangeAtIndex(1)
                let r2 = result!.rangeAtIndex(2)
                
                let link = (self as NSString).substringWithRange(r1)
                let text = (self as NSString).substringWithRange(r2)
                
                return(link,text)
            }
            
            return nil
        }
        catch{
            
            print(error)
            return nil
        }
    }
    
    
}
